package com.qf.qfsy.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class ObjectToHashCoverter {
    public static HashMap<String, Object> convertToHashMap(Object object) {
        HashMap<String, Object> hashMap = new HashMap<>();
        // 获取对象的所有字段
        Field[] fields = object.getClass().getDeclaredFields();
        // 遍历字段并将字段名和对应的值存储到HashMap中
        for (Field field : fields) {
            try {
                field.setAccessible(true); // 设置字段可访问
                Object value = field.get(object); // 获取字段值
                hashMap.put(field.getName(), value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return hashMap;
    }
}
